import {
	refundOrderDetail
} from "../api/Interface/order";
import {
	orderRefundStatus,
	orderRefundStatusConfig,
	hotelOrderAfterSaleStatus,
	orderStatus,
	orderStatusConfig,
	searchOrderStatus,
} from "../enums/order";
import {
	jumpPage
} from "../utils/route/route";
import errorCode from "../utils/http/errorCode";
import {
	useGetCurrentPageRoute
} from "./useCommon";

/**
 * 获取订单退单状态并进行逻辑操作(弃用)
 */
export const useUpdateRefund = ({
	localNo,
	orderServiceId
}) => {
	return new Promise((resolve) => {
		refundOrderDetail({
			localNo,
			orderServiceId,
		}).then((res) => {
			if (res.code === errorCode.success) {
				const config = orderRefundStatusConfig[res.data.refundStatus];
				const orderStatus = config?.dyValue;
				res.data._orderStatus = orderStatus;
				// 可申请退款,还未申请
				if (res.data.refundStatus == orderRefundStatus.can) {
					if (
						res.data.douYinPayJson &&
						res.data.douYinPayJson.item_order_info_list
					) {
						res.data.douYinPayJson.item_order_info_list.forEach((item) => {
							item.itemOrderId = item.item_order_id;
						});
					}
					resolve(res);
				}
				// 已申请过,跳转售后页面
				// new:因为已经使用了原生组件,所以不用自己解析跳转逻辑了
				else {
					resolve(res);
					// jumpPage({
					//   to: "/package-service/pages/order/afterSales/apply",
					//   mode: "redirectTo",
					//   query: {
					//     localNo,
					//     orderServiceId,
					//   },
					// });
				}
			} else resolve(res);
		});
	});
};

/**
 * 获取订单退单状态并进行逻辑操作(弃用)
 */
export const __useUpdateRefund = ({
	localNo,
	orderServiceId
}) => {
	return new Promise((resolve) => {
		refundOrderDetail({
			localNo,
			orderServiceId,
		}).then((res) => {
			if (res.code === errorCode.success) {
				const config = orderRefundStatusConfig[res.data.refundStatus];
				const orderStatus = config?.dyValue;
				// 可申请退款,还未申请
				if (res.data.refundStatus == orderRefundStatus.can) {
					res.data._orderStatus = orderStatus;
					if (
						res.data.douYinPayJson &&
						res.data.douYinPayJson.item_order_info_list
					) {
						res.data.douYinPayJson.item_order_info_list.forEach((item) => {
							item.itemOrderId = item.item_order_id;
						});
					}
					resolve(res);
				}
				// 已申请过,跳转售后页面
				// new:因为已经使用了原生组件,所以不用自己解析跳转逻辑了
				else {
					resolve(res);
					// jumpPage({
					//   to: "/package-service/pages/order/afterSales/apply",
					//   mode: "redirectTo",
					//   query: {
					//     localNo,
					//     orderServiceId,
					//   },
					// });
				}
			} else resolve(res);
		});
	});
};

/**
 * 根据不同状态跳转不同页面
 * info:订单详情
 */
export const useJumpOrderPage = (info) => {
	if (!info) return;
	const path = useGetCurrentPageRoute();
	const pages = getCurrentPages();
	const {
		options
	} = pages[pages.length - 1] ?? {
		options: {}
	};
	const {
		usejump = null
	} = options;
	const {
		status,
		orderServiceId,
		localNo,
		firstAfterSaleStatus
	} = info;
	const {
		pagePath
	} = orderStatusConfig[status] ?? {};
	console.log("onDetail 页面转发", info, path);
	if (!pagePath) return;
	if (pagePath == path) return;
	if (usejump) return;
	// 部分退款
	if (
		firstAfterSaleStatus != hotelOrderAfterSaleStatus.normal &&
		firstAfterSaleStatus != hotelOrderAfterSaleStatus.allEnd
	) {
		jumpPage({
			to: "/package-service/pages/order/afterSales/apply",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	}
	// 全部退款
	else if (firstAfterSaleStatus == hotelOrderAfterSaleStatus.allEnd) {
		jumpPage({
			to: "/package-service/pages/order/order",
			mode: "redirectTo",
			query: {
				status: searchOrderStatus.afterSales,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	} else if (status == orderStatus.awaitPay) {
		jumpPage({
			to: "/package-service/pages/order/detail",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
	} else if (status == orderStatus.finding) {
		jumpPage({
			to: "/package-service/pages/order/details/finding",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	} else if (status == orderStatus.awaitService) {
		jumpPage({
			to: "/package-service/pages/order/details/awaitService",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	} else if (status == orderStatus.awaitConfirm) {
		jumpPage({
			to: "/package-service/pages/order/details/awaitConfirm",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	} else if (status == orderStatus.awaitComment) {
		jumpPage({
			to: "/package-service/pages/order/details/final",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	} else if (status == orderStatus.final) {
		jumpPage({
			to: "/package-service/pages/order/details/final",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	} else if (status == orderStatus.serving) {
		jumpPage({
			to: "/package-service/pages/order/details/serving",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
		return;
	} else if (status == orderStatus.awaitReservation) {
		jumpPage({
			to: "/package-service/pages/order/details/awaitReservation",
			mode: "redirectTo",
			query: {
				localNo,
				orderServiceId,
				usejump: "usejump", // 已跳转过,防止无线循环跳转
			},
		});
	}
};